import java.util.Random;
import java.util.Scanner;

public class WuZiQiDemo04 {
    static String[][] qiPan = new String[10][10];

    public static void main(String[] args) throws InterruptedException {

        /*
            {{},{},{}}
            10*10(2,0) qiPan[0][2]
            * * * * * * * * * *
            * * * * * * * * * *
            * * * * * * * * * *
            * * * * * * * * * *
            * * * * * * * * * *
            * * * * * * * * * *
            * * * * * * * * * *
            * * * * * * * * * *
            * * * * * * * * * *
            * * * * * * * * * *

            1、绘制棋牌
            2、两个玩家分别落子
            3、更新棋牌
            4、判断输赢
         */
        chuShiHua();// 调用方法
//        huiZhiQiPan();// 调用绘制棋牌
        luoZi();
    }

    // 给棋牌初始化数据
    public static void chuShiHua() {
        for (int i = 0; i < qiPan.length; i++) {
            for (int j = 0; j < qiPan[i].length; j++) {
                qiPan[i][j] = "*";
            }
        }
    }


    // 绘制棋牌[{},{},{}]
    public static void huiZhiQiPan() {
        for (int i = 0; i < qiPan.length; i++) {
            for (int j = 0; j < qiPan[i].length; j++) {
                System.out.print(qiPan[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("----------------------------------------");
    }

    // 落子
    public static void luoZi() throws InterruptedException {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        int x;
        int y;

        a:
        for (int i = 0; i < 10000; i++) {// 开多少局
            Thread.sleep(1);// 休息0.001秒
            int size = 100;// 每一局的棋牌等于100
            chuShiHua();// 每一局的棋牌都应该初始化

            // 正式开始
            while (true) {
                if (flag) {
                    x = random.nextInt(10);//2
                    y = random.nextInt(10);//1
                    if (qiPan[y][x].equals("*")) {
                        qiPan[y][x] = "●";
                        size--;
                        flag = false;
                    }
                } else {
//                    System.out.println("请输入x坐标");
                    x = random.nextInt(10);//2
//                    System.out.println("请输入y坐标");
                    y = random.nextInt(10);
                    if (x >= 0 && x <= 9 && y >= 0 && y <= 9) {
                        if (qiPan[y][x].equals("*")) {
                            qiPan[y][x] = "○";
                            size--;
                            flag = true;
                        }
                    }

                }

                huiZhiQiPan();
                boolean result = shuYing(x, y);
                if (result) {
                    System.out.println(flag ? "玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢玩家赢" : "电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢电脑赢");
                    break;
                }

                if (size == 0) {// 下满了
                    System.out.println("平局");
                    break a;// 停止的for循环
                }

            }
        }


    }

    // 判断输赢
    public static boolean shuYing(int x, int y) {
        if (upDown(x, y) || leftRight(x, y) || leftUpRightDown(x, y) || rightUpLeftDown(x, y)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean upDown(int x, int y) {
        // 上
        // 最近一个子的坐标
        int count = 1;
        for (int y1 = y - 1; y1 >= 0; y1--) {
            if (qiPan[y1][x].equals(qiPan[y][x])) {
                count++;
            } else {
                break;// 停止循环的
            }
        }
        // 下
        for (int y2 = y + 1; y2 <= 9; y2++) {
            if (qiPan[y2][x].equals(qiPan[y][x])) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }
        return false;
    }

    public static boolean leftRight(int x, int y) {
        //* * * * (x-1,y) (x,y) (x+1,y) * * *
        int count = 1;
        for (int x1 = x - 1; x1 >= 0; x1--) {
            if (qiPan[y][x1].equals(qiPan[y][x])) {
                count++;
            } else {
                break;
            }
        }

        for (int x2 = x + 1; x2 <= 9; x2++) {
            if (qiPan[y][x2].equals(qiPan[y][x])) {
                count++;
            } else {
                break;
            }
        }

        if (count >= 5) {
            return true;
        }

        return false;

    }

    public static boolean leftUpRightDown(int x, int y) {
        // 左上
        int count = 1;
        for (int x1 = x - 1, y1 = y - 1; x1 >= 0 && y1 >= 0; x1--, y1--) {
            if (qiPan[y1][x1].equals(qiPan[y][x])) {
                count++;
            } else {
                break;
            }

        }
        // 右下
        for (int x2 = x + 1, y2 = y + 1; x2 <= 9 && y2 <= 9; x2++, y2++) {
            if (qiPan[y2][x2].equals(qiPan[y][x])) {
                count++;
            } else {
                break;
            }
        }

        if (count >= 5) {
            return true;
        }

        return false;

    }

    public static boolean rightUpLeftDown(int x, int y) {
        int count = 1;
        // 右上
        for (int x1 = x + 1, y1 = y - 1; x1 <= 9 && y1 >= 0; x1++, y1--) {
            if (qiPan[y1][x1].equals(qiPan[y][x])) {
                count++;
            } else {
                break;
            }

        }
        //左下
        for (int x2 = x - 1, y2 = y + 1; x2 >= 0 && y2 <= 9; x2--, y2++) {
            if (qiPan[y2][x2].equals(qiPan[y][x])) {
                count++;
            } else {
                break;
            }

        }
        if (count >= 5) {
            return true;
        }

        return false;
    }
}
